<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="start.fly.test.database.databaseTool"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title></title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">


</head>

<body>

	<%
	if (request.getParameter("product_id") != null) {
		String username = (String) session.getAttribute("username");
		
		//System.out.println("Message:"+username);
		
			String ProductID = request.getParameter("product_id");
			//System.out.println("Message:"+ProductID);
			
			//根据ProductID获取商品的金额
			float product_Money = databaseTool.queryMoneyByProductID(ProductID);
			//System.out.println("Message:"+product_Money);
			
			//查询用户的cardid
			int CardID = databaseTool.queryCardIDByUsername(username);

			//System.out.println("Message:"+CardID);
			//根据得到的CardID获得点卡金额Money
			float card_Money = databaseTool.queryMoneyByCardID(CardID);

			

			//点卡金额减去购买商品的金额
			float new_Money = card_Money - product_Money;

			if(new_Money >= 0){
				//保存剩余的金额于点卡表中
				boolean isUpdate = databaseTool.updateMoneyToClickCard(CardID,
						new_Money);
				boolean isInsert = databaseTool.insertToTransaction(ProductID, username);

				if (isUpdate) {
					out.print("<script>alert('购买成功');document.location='user/getProduct.jsp'</script>");
				} else {
					out.print("<script>alert('购买失败');document.location='user/getProduct.jsp'</script>");
				}
			}else{
				out.print("<script>alert('点卡金额不足，请充值');document.location='getProduct.jsp'</script>");
			}
			
	}
		
	%>
</body>
</html>
